package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    public StudentDao studentDao= new StudentDao();
    /**
     * 增加
     * @param stu
     * @return
     */
    public boolean addStudent(Student stu) {
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
       Student[]stus=studentDao.findAllStudent();
        //假设在数组中不存在
        boolean exists=false;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student!=null&&student.getId().equals(id)){
                exists=true;
                break;
            }
        }
        return exists;
    }
    /**
     * 业务员的查询方法
     * @return
     * 调用库管对象的findAllStudent获取学生对相爱那个数组
     * 判断数组中是否有学生信息(有:返回地址,没有返回null)
     */
    public Student[] findAllStudent() {
        Student[]allStudent=studentDao.findAllStudent();
        //判断数组中是否有学生信息(有:返回地址 没有:返回null)
        //思路: 数组中只要有一个不是null的元素,那么就代表有学生信息
        boolean flag=false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            if (stu!=null){
                flag=true;
                break;
            }
        }
        if (flag){
            return allStudent;
        }else {
            return null;
        }
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
